/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


function MyTools() {

}
;
MyTools.fillDateVal = {
    content: "#data-content",
    data: {},
    matchAttr: "data-field",
    notFillFlag: "mytools-not-fill",
    notFillAttrVals: null,
    keyValDataName: "key-val"
};

/**
 * 填充数据到HTML文件模板中
 * @param {Object} args{<br>
 * content:选择器 包含需要填充信息的元素的包含层 默认:#data-content<br>
 * data:js 对象 需要填充的数据对象 默认{}<br>
 * matchAttr:填充时查找的元素的匹配属性名 默认 data-field <br>
 * notFillFlag:元素上标明的不要填充的选择器 默认 mytools-not-fill<br>
 * notFillAttrVals:数组 包含不需要填充的匹配元素的值得数组 默认null<br>
 * keyValDataName:存储映射对象的data属性名称不包括data前缀 默认 key-val<br>
 * }<br>
 * @returns {undefined}
 */
MyTools.fillDateToHtml = function (args) {
    var param = $.extend({}, MyTools.fillDateVal, args)
    var content = param.content;
    var data = param.data;
    var matchAttr = param.matchAttr;
    var notFillFlag = param.notFillFlag;
    var notFillAttrVals = param.notFillAttrVals;
    var keyValDataName = param.keyValDataName;
    var eles = $(content).find("[" + matchAttr + "]");
    $(eles).each(function (index, ele) {
        //是否符合填充数据到元素的条件
        var isToFill = true;
        var matchAttrVal = $(ele).attr(matchAttr);
        //该元素是否不需要填充
        if ($(ele).is(notFillFlag)) {
            isToFill = false;
        }
        //该元素是否在不需要填充的数据列表中
        if (notFillAttrVals && isToFill === true) {
            if ($.inArray(matchAttrVal, notFillAttrVals) !== -1) {
                isToFill = false;
            }
        }
        if (isToFill === true) {
            //存储最终的数据值
            var dataVal = null;
            //尝试直接获取数据值
            if (data[matchAttrVal]!==null && typeof(data[matchAttrVal]) !== "undefined") {
                dataVal = data[matchAttrVal];
            } else {
                //尝试逐级获取数据值
                var splits = matchAttrVal.split(".");
                var splength = splits.length;
                var tmpVal = null;
                if (splength > 0) {
                    tmpVal = data[splits[0]];
                }
                for (var si = 1; si < splength; si++) {
                    var str = splits[si];
                    if (tmpVal) {
                        if (si === (splength - 1)) {
                            dataVal = tmpVal[str];
                        } else {
                            tmpVal = tmpVal[str];
                        }
                    }
                }
            }
            //填充数据到元素中
            if (dataVal!==null && typeof(dataVal)!=="undefined") {
                if ($(ele).is("input")) {
                    if ($(ele).is("[type='radio']") || $(ele).is("[type='checkbox']")) {
                        if ($(ele).val() === dataVal) {
                            $(ele).attr("checked", "true");
                        }
                    } else {
                        $(ele).val(dataVal);
                    }
                } else if ($(ele).is("select") || $(ele).is("textarea")) {
                    $(ele).val(dataVal);
                } else {
                    if ($(ele).is("[data-" + keyValDataName + "]")) {
                        var keyValObj = $(ele).data(keyValDataName);
                        var realVal = keyValObj[dataVal];
                        $(ele).html(realVal);
                    } else {
                    	if(dataVal == ""){
                    		dataVal = "&#12288";
                    	}
                        $(ele).html(dataVal);
                    }
                }
            }
        }
    });
}
/**
 * 将带有横杠的字符串转换为驼峰形式
 * @param {String} lineStr  原始的字符串，一般是带有横杠的字符串
 * @returns {String} 驼峰形式的字符串
 */
MyTools.strLineToHump = function (lineStr) {
    var strs = lineStr.split("-");
    var rsStr = "";
    if (strs.length > 0) {
        rsStr = strs[0];
    }
    if (strs.length > 1) {
        for (var i = 1; i < strs.length; i++) {
            var tmpStr = strs[i];
            if (tmpStr) {
                var tmpChar = tmpStr.substring(0, 1).toUpperCase();
                var postString = tmpStr.substring(1);
                var tmpStr = tmpChar + postString;
                rsStr += tmpStr;
            }
        }
    }
    return rsStr;
};

MyTools.addLastToCloneNode = function (cloneNode, lastFix) {
    $(cloneNode).find("[id]").each(function (index, domEle) {
        var idVal = $(domEle).attr("id");
        $(domEle).attr("id", idVal + lastFix);
    });
    $(cloneNode).find("[for]").each(function (index, domEle) {
        var idVal = $(domEle).attr("for");
        $(domEle).attr("for", idVal + lastFix);
    });
}
MyTools.alertVal = {
    alertTmp: "#alert-tmp .alert",
    inforContent: ".hs-alert-infor",
    alertShow: "#alert-show",
    showTime:5000
};
MyTools.showAlert=function(alertClass, alertInfor) {
	$("#alert-tmp").html(" ");
    var alertTmp = MyTools.alertVal.alertTmp;
    var cloneTmp = $(alertTmp).clone(true);
    $(cloneTmp).addClass(alertClass);
    $(cloneTmp).find(MyTools.alertVal.inforContent).html(alertInfor);
    var idStr = Math.round(Math.random() * 100000000);
    idStr = idStr + "-clone";
    $(cloneTmp).attr("id", idStr);
    $(MyTools.alertVal.alertShow).append(cloneTmp);
    setTimeout(function (e) {
        $("#" + idStr).alert('close');
    }, MyTools.alertVal.showTime);
}

/**
 * 打印函数<br>
 * @param args {<br>
 * printSelector:打印区域选择器 默认为"#print-div" ,<br>
 * printTitle: 打印标题 "",<br>
 * styleLink:样式组 "",<br>
 * baseUri:base 基准url 默认为null<br>
 *}
 */
MyTools.printDiv=function(args)
{
    var defVal={
        printSelector:"#print-div",
        printTitle:"标题",
        styleLink:[],
        styleStr:"",
        baseUri:null
    }
    var param=$.extend({},defVal,args);
    var win=window.open();
    
    var head = win.document.getElementsByTagName("head")[0];
    for(var ind in param.styleLink) {
    	linkTag = document.createElement('link');
    	linkTag.setAttribute('rel','stylesheet');
    	linkTag.setAttribute('type','text/css');
        linkTag.href = param.styleLink[ind];
        head.appendChild(linkTag);
	}
    var headstr = "<title>"+param.printTitle+"</title>";
    if(param.baseUri) {
        headstr+="<base href=\""+param.baseUri+"\"></base>";
    }
    
    win.document.title = param.printTitle;
    win.document.body.innerHTML = $(param.printSelector).html();
    
    win.print();
    //win.close();
    return false;
}
